package hn.sigit.view;

import hn.sigit.logic.general.ResourceBundleHelper;
import hn.sigit.logic.general.impl.ResourceBundleHelperImpl;
import hn.sigit.model.hnd.administrative.HND_MunicipalTransactionType;
import hn.sigit.model.hnd.cadastre.HND_DocumentType;
import hn.sigit.model.hnd.cadastre.HND_EasementType;
import hn.sigit.model.hnd.cadastre.HND_TaxStatusType;
import hn.sigit.model.ladm.administrative.LA_AdministrativeSourceType;
import hn.sigit.model.ladm.administrative.LA_ResponsibilityType;
import hn.sigit.model.ladm.administrative.LA_RestrictionType;
import hn.sigit.model.ladm.administrative.LA_RightType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;


@Name("uiConverters")
@Scope(value = ScopeType.APPLICATION)
@AutoCreate
public class UIConverters {
	private final EnumTypeConverter<LA_AdministrativeSourceType> administrativeSourceType;
	private final EnumTypeConverter<HND_DocumentType> documentType;
	private final EnumTypeConverter<HND_EasementType> easementType;
	private final EnumTypeConverter<HND_TaxStatusType> taxStatusType;
	private final EnumTypeConverter<LA_RightType> rightType;
	private final EnumTypeConverter<LA_RestrictionType> restrictionType;
	private final EnumTypeConverter<LA_ResponsibilityType> responsibilityType;
	private final EnumTypeConverter<HND_MunicipalTransactionType> municipalTransactionType;
	
	private final ResourceBundleHelper resBundle;
	private final String DEFAULT_OPTION;
	

	public UIConverters() {
		resBundle = new ResourceBundleHelperImpl();
		DEFAULT_OPTION = resBundle.loadMessage("txt.select_item");

		administrativeSourceType = new EnumTypeConverter<LA_AdministrativeSourceType>(LA_AdministrativeSourceType.class);
		documentType = new EnumTypeConverter<HND_DocumentType>(HND_DocumentType.class);
		easementType = new EnumTypeConverter<HND_EasementType>(HND_EasementType.class);
		taxStatusType = new EnumTypeConverter<HND_TaxStatusType>(HND_TaxStatusType.class);
		rightType = new EnumTypeConverter<LA_RightType>(LA_RightType.class);
		restrictionType = new EnumTypeConverter<LA_RestrictionType>(LA_RestrictionType.class);
		responsibilityType = new EnumTypeConverter<LA_ResponsibilityType>(LA_ResponsibilityType.class);
		municipalTransactionType = new EnumTypeConverter<HND_MunicipalTransactionType>(HND_MunicipalTransactionType.class);
	}
	
	public EnumTypeConverter<LA_AdministrativeSourceType> getAdministrativeSourceType() {
		return administrativeSourceType;
	}
	public EnumTypeConverter<HND_DocumentType> getDocumentType() {
		return documentType;
	}
	public EnumTypeConverter<HND_EasementType> getEasementType() {
		return easementType;
	}
	public EnumTypeConverter<HND_TaxStatusType> getTaxStatusType() {
		return taxStatusType;
	}
	public EnumTypeConverter<LA_RightType> getRightType() {
		return rightType;
	}
	public EnumTypeConverter<LA_RestrictionType> getRestrictionType() {
		return restrictionType;
	}
	public EnumTypeConverter<LA_ResponsibilityType> getResponsibilityType() {
		return responsibilityType;
	}
	public EnumTypeConverter<HND_MunicipalTransactionType> getMunicipalTransactionType() {
		return municipalTransactionType;
	}



	public class EnumTypeConverter<E extends Enum<E>> implements Converter {
		private Class<E> clazz;
		private List<SelectItem> valueList;
		
		public EnumTypeConverter(Class<E> clazz) {
			this.clazz = clazz;
			
			this.valueList = new ArrayList<SelectItem>();
			valueList.add(new SelectItem("", DEFAULT_OPTION));
			for (E e : clazz.getEnumConstants())
				valueList.add(new SelectItem(e, resBundle.loadMessage(e.name())));
		}

		@Override
		public Object getAsObject(FacesContext context, UIComponent component,
				String value) {
			value = value.trim();
			if (value.equals("") || value.equals(DEFAULT_OPTION))
				return null;
			return Enum.valueOf(clazz, value);
		}

		@Override
		public String getAsString(FacesContext context, UIComponent component,
				Object value) {
			if (value instanceof String) {
				if (((String) value).trim().equals(""))
					return DEFAULT_OPTION;
				else
					return value.toString();
			}
			return ((Enum<?>) value).name();
		}
		
		public List<SelectItem> getValueList() {
			return Collections.unmodifiableList(valueList);
		}
	}
}
